package com.ldcc.report_system.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.ldcc.report_system.ApplicationConfig;
import com.ldcc.report_system.R;
import com.ldcc.report_system.adapter.FragmentViewPagerAdapter;
import com.ldcc.report_system.databinding.ActivityMainBinding;
import com.ldcc.report_system.ui.report.ReportListFragment;
import com.ldcc.report_system.ui.report.SelectAddReportFragment;
import com.ldcc.report_system.ui.user.PersonalCenterFragment;
import com.ldcc.report_system.util.ActivitySkipUtil;
import com.ldcc.report_system.util.WindowUtils;

import java.util.ArrayList;


/**
 * @author wwj[yeshengkele@outlook.com]
 * @description 主界面
 */
public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity";
    // 目标fragment的key值
    public static final String TARGET_FRAGMENT = "target_fragment";
    private ActivityMainBinding mainBinding;

    private ImageView onIndexIcon;
    private TextView onIndexText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 为绑定赋值
        this.mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(this.mainBinding.getRoot());
        // 检查用户是否登录
        ActivitySkipUtil.checkUserToLogin(this);
        // 检查用户是否第一次使用软件
        ActivitySkipUtil.checkFirstUse(this);
        // 设置界面顶部为透明
        WindowUtils.setWindowTopTransparent(getWindow());
        this.initPage();
    }

    private void initPage() {
        // 赋值初始值
        this.onIndexIcon = this.mainBinding.bottomBtn.showReportBtnIcon;
        this.onIndexText = this.mainBinding.bottomBtn.showReportBtnText;

        // 初始化viewPager2
        this.mainBinding.mainViewPager.setAdapter(new FragmentViewPagerAdapter(
                new ArrayList<Fragment>() {{
                    add(new ReportListFragment());
                    add(new SelectAddReportFragment());
                    // add(new UserInfoFragment(MainActivity.this));
                    add(new PersonalCenterFragment(MainActivity.this));
                }}, getSupportFragmentManager(), getLifecycle()
        ));
        //true:滑动，false：禁止滑动
        this.mainBinding.mainViewPager.setUserInputEnabled(false);

        // 看日报 响应事件
        this.mainBinding.bottomBtn.showReportBtn.setOnClickListener(view -> {
            mainBinding.mainViewPager.setCurrentItem(ApplicationConfig.SHOW_REPORT_FRAGMENT, false);
            btnSelected(mainBinding.bottomBtn.showReportBtnIcon, mainBinding.bottomBtn.showReportBtnText);
        });

        //添加日报事件
        this.mainBinding.bottomBtn.addReportBtn.setOnClickListener(view -> {
            mainBinding.mainViewPager.setCurrentItem(ApplicationConfig.ADD_REPORT_FRAGMENT, false);
            btnSelected(mainBinding.bottomBtn.addReportIcon, mainBinding.bottomBtn.addReportBtnText);
        });

        // 个人中心
        this.mainBinding.bottomBtn.userInfoBtn.setOnClickListener(view -> {
            mainBinding.mainViewPager.setCurrentItem(ApplicationConfig.USER_INFO_FRAGMENT, false);
            btnSelected(mainBinding.bottomBtn.userInfoIcon, mainBinding.bottomBtn.userInfoBtnText);
        });

        // 设置默认值
        switch (getIntent().getIntExtra(MainActivity.TARGET_FRAGMENT, 0)) {
            case 1:
                mainBinding.mainViewPager.setCurrentItem(ApplicationConfig.ADD_REPORT_FRAGMENT, false);
                this.btnSelected(mainBinding.bottomBtn.addReportIcon, mainBinding.bottomBtn.addReportBtnText);
                break;
            case 2:
                mainBinding.mainViewPager.setCurrentItem(ApplicationConfig.USER_INFO_FRAGMENT, false);
                this.btnSelected(mainBinding.bottomBtn.userInfoIcon, mainBinding.bottomBtn.userInfoBtnText);
                break;
            default:
                mainBinding.mainViewPager.setCurrentItem(ApplicationConfig.SHOW_REPORT_FRAGMENT, false);
                // 默认看日报高亮
                this.btnSelected(mainBinding.bottomBtn.showReportBtnIcon, mainBinding.bottomBtn.showReportBtnText);
        }
    }


    /**
     * @param view 图片
     * @param text 文字
     * @description 按钮点下
     */
    private void btnSelected(ImageView view, TextView text) {
        // 复位
        onIndexIcon.setSelected(false);
        onIndexText.setTextColor(ContextCompat.getColor(this, R.color.normal));
        // 选中
        view.setSelected(true);
        text.setTextColor(ContextCompat.getColor(this, R.color.activation));
        // 赋值
        onIndexIcon = view;
        onIndexText = text;
    }
}